<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/login-reg.css">
    <script src="../js/jquery.mini.js"></script>
    <script src="../js/login-reg.js"></script>
</head>

<body>
    <div class="center-box">
        <div class="center-box-content">
            <div class="note-box">
                <div class="note">
                    <h2>大学城二手交易平台</h2>
                    <p>旨在为大学城的学子提供一个交换闲置物品的平台</p>
                    <p>在这里，你可以发布闲置物品、在线与客户聊天、发布帖子、与同好讨论最新的潮流</p>
                    <p> 更多的惊喜等待你发现……</p>
                </div>
            </div>
            <div class="login-reg-box">
                <div class="login-box">
                    <form class="login-form-list">
                        <ul class="login-table">
                            <li><input type="text" name="nickname" id="username" placeholder="用户名"> </li>
                            <li><input type="password" name="password" placeholder="密码"> </li>
                            <li>
                                <div id="login-btn">登录</div>
                            </li>
                            <li>没有账号？点击<span id="reg">注册</span></li>
                        </ul>
                    </form>
                </div>
            </div>
            <div class="reg-form-box">
                <div class="reg-form">
                    <form class="reg-form-list">
                        <div class="form-item">
                            <label for="nickname">用户昵称:</label>
                            <input type="text" name="nickname" id="nickname">
                        </div>
                        <div class="form-item set-password">
                            <label for="password">设置密码:</label>
                            <input type="password" name="password" id="password">
                            <div class="inform">
                                <div class="inform-text"></div>
                            </div>
                        </div>
                        <div class="form-item check-password">
                            <label for="check-password">确认密码:</label>
                            <input type="password" name="check-password" id="check-password">
                            <div class="inform-sec">
                                <div class="inform-sec-text"></div>
                            </div>
                        </div>
                        <div class="form-item">
                            <div id="reg-btn">注册</div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
<script>
    $('#login-btn').click(function () {
        console.log("click");
        $.ajax({
            type: "POST",
            url: "/users/login",
            data: $('.login-form-list').serialize(),
            dataType: "json",
            success: function (result) {
                if (result.state === 200) {
                    alert("登录成功");
                    // sessionStorage.setItem("username", result.username);
                    sessionStorage.setItem("imgpath", result.data.img_path);
                    sessionStorage.setItem("userid", result.user_id);
                    window.location.href = "../index.html";
                } else {
                    alert(result.message);

                }


            },
            error: function (result) {
                alert(result.message);
            }
        });
    });
    $('#reg-btn').click(function () {
        console.log("click");
        $.ajax({
            type: "POST",
            url: "/users/reg",
            data: $('.reg-form-list').serialize(),
            dataType: "json",
            success: function (result) {
                if (result.state === 200) {
                    alert("注册成功，快去登录吧！");
                    $('.reg-form-box').hide();
                    $('.login-reg-box').show();
                } else {
                    alert(result.message);
                }
            },
            error: function () {

            }
        });
    });
</script>

</html>